﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GearSoket : MonoBehaviour
{
    public Animator MyAnimator { get; set; }
    protected SpriteRenderer spriteRenderer;
    private AnimatorOverrideController overrideController;
    private Animator parentAnimator;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        parentAnimator = GetComponentInParent<Animator>();
        MyAnimator = GetComponent<Animator>();
        overrideController = new AnimatorOverrideController(MyAnimator.runtimeAnimatorController);
        MyAnimator.runtimeAnimatorController = overrideController;
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Equip(AnimationClip[] animations)
    {
        UnEquip();
        spriteRenderer.color = Color.white;
        //casts
        overrideController["castDown"] = animations[0];
        overrideController["castLeft"] = animations[1];
        overrideController["castRight"] = animations[2];
        overrideController["castUp"] = animations[3];
        //idles
        overrideController["idleDown"] = animations[4];
        overrideController["idleLeft"] = animations[5];
        overrideController["idleRight"] = animations[6];
        overrideController["idleUp"] = animations[7];
        //walks
        overrideController["MoveDown"] = animations[8];
        overrideController["MoveLeft"] = animations[9];
        overrideController["MoveRight"] = animations[10];
        overrideController["MoveUp"] = animations[11];
     
        
    }
    public void UnEquip()
    {
        //casts
        overrideController["castDown"] = null;
        overrideController["castLeft"] = null;
        overrideController["castRight"] = null;
        overrideController["castUp"] = null;
        //idles
        overrideController["idleDown"] = null;
        overrideController["idleLeft"] = null;
        overrideController["idleRight"] = null;
        overrideController["idleUp"] = null;
        //walks
        overrideController["walkDown"] = null;
        overrideController["walkLeft"] = null;
        overrideController["walkRight"] = null;
        overrideController["walkUp"] = null;

        spriteRenderer.color = new Color(0,0,0,0);
    }
    public virtual void setAxis(float x, float y)
    {
        MyAnimator.SetFloat("x", x);
        MyAnimator.SetFloat("y", y);
    }
    public void activeLayerController(string layerName)
    {
        for (int i = 0; i < MyAnimator.layerCount; i++)
        {
            MyAnimator.SetLayerWeight(i, 0);
        }
        MyAnimator.SetLayerWeight(MyAnimator.GetLayerIndex(layerName), 1);
    }
}
